﻿using UnityEngine;
using UnityEngine.UI;
using EasySM;
using System.Collections.Generic;
using System;
using System.Collections;

public class ChatComponent : MonoBehaviour
{
    private BottomComponent mBottom;
    private TopComponent mTop;
    private MiddleComponent mMiddle;

    void Awake()
    {
        mBottom = AddComponent<BottomComponent>.Create(transform.Find("BottomBg"));
        mMiddle = AddComponent<MiddleComponent>.Create(transform.Find("MiddleBg"));
        mTop = AddComponent<TopComponent>.Create(transform.Find("TopBg"));
    }
}

public class AddComponent<T> where T : BaseComponent
{
    public static T Create(Transform target)
    {
        if (target == null) return null;

        T t = target.GetComponent<T>();
        if (t == null) t = target.gameObject.AddComponent<T>();
        t.Init();
        return t;
    }
}

public abstract class BaseComponent : MonoBehaviour
{
    public abstract void Init();
}

public class BottomComponent : BaseComponent
{
    private enum EUI_State
    {
        Null,
        EMessage,
        EVoice,
    }

    private Button mSendMessageBtn;
    private Button mFaceBtn;
    private Button mVoiceBtn;
    private Button mMessageBtn;
    private InputField mInputFiled;
    private UILongPressButton mVoicePress;

    private EasyStateMachine<EUI_State> mState;

    public static BottomComponent Create(Transform target)
    {
        if (target == null) return null;

        BottomComponent b = target.GetComponent<BottomComponent>();
        if (b == null) b = target.gameObject.AddComponent<BottomComponent>();
        b.Init();
        return b;
    }

    public override void Init()
    {
        mSendMessageBtn = transform.Find("OptionBtn").GetComponent<Button>();
        mSendMessageBtn.onClick.AddListener(OnSendMessageClick);

        mFaceBtn = transform.Find("FaceBtn").GetComponent<Button>();
        mFaceBtn.onClick.AddListener(OnFaceClick);

        mVoiceBtn = transform.Find("VoiceBtn").GetComponent<Button>();
        mVoiceBtn.onClick.AddListener(OnVoiceClick);

        mVoicePress = transform.Find("VoicePress").GetComponent<UILongPressButton>();
        mVoicePress.onPress += OnVoicePress;
        mVoicePress.onPressOver += OnVoicePressOver;
        mVoicePress.onPressStart += OnVoicePressStart;

        mMessageBtn = transform.Find("MessageBtn").GetComponent<Button>();
        mMessageBtn.onClick.AddListener(OnMessageClick);

        mInputFiled = transform.Find("MessageInput").GetComponent<InputField>();

        mState = EasyStateMachine<EUI_State>.Initialize(this, EUI_State.Null);
        mState.ChangeState(EUI_State.EMessage);
    }

    private void OnVoicePressStart()
    {
        Debug.LogError("Press Start!");
    }

    private void OnVoicePressOver(float duration)
    {
        Debug.LogError("Press Over!  Duration:" + duration);
    }

    private void OnVoicePress()
    {
        Debug.LogError("OnPress");
    }

    private void EMessage_Enter()
    {
        Debug.LogError("Change to message state!");

        mInputFiled.gameObject.SetActive(true);
        mVoiceBtn.gameObject.SetActive(true);

        mVoicePress.gameObject.SetActive(false);
        mMessageBtn.gameObject.SetActive(false);
    }

    private void EVoice_Enter()
    {
        Debug.LogError("Change to voice state!");

        mVoicePress.gameObject.SetActive(true);
        mMessageBtn.gameObject.SetActive(true);

        mInputFiled.gameObject.SetActive(false);
        mVoiceBtn.gameObject.SetActive(false);
    }

    private void OnSendMessageClick()
    {
        Debug.LogError("发送消息点击");
        string content = mInputFiled.text;
    }

    private void OnFaceClick()
    {
        Debug.LogError("表情按钮点击");
    }

    private void OnVoiceClick()
    {
        if (mState.State == EUI_State.EVoice) return;

        mState.ChangeState(EUI_State.EVoice);
    }

    private void OnMessageClick()
    {
        if (mState.State == EUI_State.EMessage) return;

        mState.ChangeState(EUI_State.EMessage);
    }
}

public class TopComponent : BaseComponent
{
    private Button mBackBtn;

    public override void Init()
    {
        mBackBtn = transform.Find("BackBtn").GetComponent<Button>();
        mBackBtn.onClick.AddListener(OnBackClick);
    }

    private void OnBackClick()
    {
        Debug.LogError("返回按钮点击");
    }
}

public class MiddleComponent : BaseComponent
{
    private const int C_MinHeight = 120;

    private VariableScroll mScroll;
    public int InitCount = 1;
    public int AddCount = 1;
    private InlineText mText;

    public string[] Texts = new string[] {
       "Click Send Button Send Message,[#emoji_8]&&[1#run]000",
       "<color=black>多图集表情:</color>\nNewText[0#emoji_0] New Text[1#yawn]NN\nNewText[1#walk] New Text[0#emoji_8]NN",
       "<color=black>超链接:</color>\nNewText[0#emoji_0] New Text[1#yawn]NN\nNewText[-101#超链接] NewText\nNewText[1#walk] New Text[0#emoji_8]NN"
    };

    public override void Init()
    {
        mText = transform.Find("Mask/Content/MessageItem/Text").GetComponent<InlineText>();

        mScroll = transform.GetComponent<VariableScroll>();

        mScroll.Init();
        //AudioSource audio = null;
        //audio.time = 0;
        //AddDatas();

        //List<MessageBaseData> datas = new List<MessageBaseData>();
        //for (int i = 0; i < 50; i++)
        //{
        //    datas.Add(new MessageBaseData());
        //}
        //mScroll.AddDatas(datas);
    }

    private int GetHeight(string content)
    {
        mText.text = content;
        return Mathf.RoundToInt(mText.preferredHeight) + 12;
    }

    private void AddDatas()
    {
        List<MessageBaseData> datas = new List<MessageBaseData>();
        string content = string.Empty;
        for (int i = 0; i < InitCount; i++)
        {
            content = Texts[UnityEngine.Random.Range(0, 3)];
            int height = GetHeight(content);
            datas.Add(new MessageBaseData(height, content));
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            List<MessageBaseData> datas = new List<MessageBaseData>();
            string content = string.Empty;
            for (int i = 0; i < AddCount; i++)
            {
                content = Texts[UnityEngine.Random.Range(0, 3)];
                int height = GetHeight(content);
                datas.Add(new MessageBaseData(height, content));
            }
            mScroll.AddDatas(datas);
        }
    }
}